#include "gwtext.h"

#include <QPainter>
#include <QDebug>

GwText::GwText()
{
    this->createPropertyList();
}

GwText::GwText(const GwText &other)
{
    this->createPropertyList();
    this->setFont(other.font());
    this->setPlainText(other.toPlainText());
    this->setPos(other.pos());
    this->setDefaultTextColor(other.defaultTextColor());
}


void GwText::setPropertyTree(QtTreePropertyBrowser *treepro)
{
    QtProperty *item;
    treepro->clear();
    foreach(item, m_propertys)
    {
        this->refreshProperty(item);
        treepro->addProperty(item);
    }
    treepro->setFactoryForManager(m_pVarManager,m_pVarFactory);
}

void GwText::refreshProperty(QtProperty *item)
{
    qDebug()<<"refresh:"<<this->x()<<this->y();
    if(item->propertyName()==QStringLiteral("字体"))
        m_pVarManager->setValue(item,this->font());
    else if(item->propertyName()==QStringLiteral("X"))
        m_pVarManager->setValue(item,this->x());
    else if(item->propertyName()==QStringLiteral("Y"))
        m_pVarManager->setValue(item,this->y());
    else if(item->propertyName()==QStringLiteral("颜色"))
        m_pVarManager->setValue(item,this->defaultTextColor());
}

void GwText::refreshPropertyList()
{
    QtProperty *item;
    foreach (item, m_propertys) {
        this->refreshProperty(item);
    }
}

void GwText::createPropertyList()
{
    if(m_pVarManager!=NULL)
    {
        m_pVarManager->clear();
        delete m_pVarManager;
    }
    if(m_pVarFactory!=NULL)
    {
        delete m_pVarFactory;
    }
    this->m_pVarManager =new QtVariantPropertyManager();
    this->m_pVarFactory=new QtVariantEditorFactory();

    QtVariantProperty *item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("X"));
    m_pVarManager->setValue(item,this->x());
    m_propertys.append(item);

    item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Y"));
    m_pVarManager->setValue(item,this->y());
    m_propertys.append(item);

    item =m_pVarManager->addProperty(QVariant::Font, QStringLiteral("字体"));
    m_pVarManager->setValue(item,this->font());
    m_propertys.append(item);

    item =m_pVarManager->addProperty(QVariant::Color, QStringLiteral("颜色"));
    m_pVarManager->setValue(item,this->defaultTextColor());
    m_propertys.append(item);

    connect(m_pVarManager,&QtVariantPropertyManager::valueChanged,this,&GwText::propertyChanged);
}

GwText &GwText::operator =(const GwText &other)
{
    this->setFont(other.font());
    this->setPlainText(other.toPlainText());
    this->setPos(other.pos());
    this->setDefaultTextColor(other.defaultTextColor());
//    this->setHtml(other.toHtml());
    return *this;
}

void GwText::setConfig()
{
    this->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
    this->setAcceptHoverEvents(true);
}

QDataStream & operator <<(QDataStream &out, const GwText &data)
{
//    out<<data.x()<<data.y()<<data.font()<<data.toPlainText();
    out<<data.x()<<data.y()<<data.font()<<data.toPlainText()<<data.defaultTextColor();
    return out;
}

QDataStream & operator >>(QDataStream &in, GwText &data)
{
    qreal x,y;
    QFont ofont;QString txt;
    QColor color;
    in>>x>>y>>ofont>>txt>>color;
//    in>>x>>y>>ofont>>txt;
    data.setPos(x,y);
    data.setFont(ofont);
    data.setPlainText(txt);
//    data.setHtml(ohtml);
    data.setDefaultTextColor(color);
    return in;
}

void GwText::propertyChanged(QtProperty *property, const QVariant &val)
{
    if(property->propertyName()==QStringLiteral("X")){
        this->setX(val.toInt());
    }
    else if(property->propertyName()==QStringLiteral("Y")){
        this->setY(val.toInt());
    }
    else if(property->propertyName()==QStringLiteral("字体")){
        this->setFont(val.value<QFont>());
    }
    else if(property->propertyName()==QStringLiteral("颜色")){
        this->setDefaultTextColor(val.value<QColor>());
    }
}

void GwText::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if(this->flags().testFlag(QGraphicsItem::ItemIsSelectable))
        this->setTextInteractionFlags(Qt::TextEditorInteraction);
    QGraphicsTextItem::mouseDoubleClickEvent(event);
}


void GwText::focusOutEvent(QFocusEvent *event)
{
    this->setTextInteractionFlags(Qt::NoTextInteraction);
    QGraphicsTextItem::focusOutEvent(event);
}


void GwText::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    this->refreshPropertyList();
    QGraphicsTextItem::mouseMoveEvent(event);
}
